//? 定义初始状态
const initialState = {
    count: 0,
};
//? 定义 mutation 类型
const INCREMENT = 'INCREMENT';
const DECREMENT = 'DECREMENT';
//? 定义 mutation 参数state 用于改变state
function incrementMutation(state) {
    state.count++;
}
function decrementMutation(state) {
    state.count--;
}
//? 创建 store
let store = {
    state: initialState,
    mutations: {
        [INCREMENT]: incrementMutation,
        [DECREMENT]: decrementMutation,
    },
    actions: {
        increment(context) {
            context.commit(INCREMENT);
        },
        decrement(context) {
            context.commit(DECREMENT);
        },
    },
    subscribers: [],
    getState() {
        return this.state;
    },
    //? 提交mutation
    commit(mutationType) {
        this.mutations[mutationType](this.state);
        this.subscribers.forEach(subscriber => subscriber());
    },
    dispatch(actionType) {
        this.actions[actionType](this);
    },
    //? 订阅
    subscribe(subscriber) {
        this.subscribers.push(subscriber);
    },
};
//? 订阅 state 变化
store.subscribe(() => {
    console.log(store.getState());
});
//? 提交 mutation
store.dispatch('increment');
store.dispatch('increment');
store.dispatch('decrement');
